package com.cec.handler;

import com.cec.exception.BaseException;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/***
 * 全局异常错误
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(BaseException.class)
    public Result defaultExceptionHandler(BaseException e) {
        logException(e);
        return Result.error(e.getResultCode());
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result HttpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        logException(e);
        return Result.error(ResultCode.PARAM_ERROR);
    }
/*    @ExceptionHandler(Exception.class)
    public Result GlobalException(Exception e) {
        logException(e);
        System.out.println("e = " + e);
        return Result.error(500,"系统错误");
    }*/
    private void logException(Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("捕获到请求异常", e);
        } else {
            log.info("捕获到请求异常, {}", e.getMessage());
            //log.info("捕获到请求异常, {}", );
        }
    }
}
